//
//  AYProductVm.swift
//  AYHome
//
//  Created by amg on 2021/4/6.
//

import RxSwift
import AYNetWorkCore
import SwiftyJSON
import AYUtilCore
import Result


class AYProductVm : AYBaseListVm{
    /// 表示正在刷新中
    let loading = AYActivityIndicator()
    /// 表示在下来刷新中
    let pullLoading = AYActivityIndicator()
    /*
     接收返回的数据
     */
    /// 返回首页数据
    let productsElements = BehaviorSubject<[AYProductModel]>(value: [])
    /// 表示正在刷新中
    let refreshing = BehaviorSubject(value:false)
    
    init(input: (
        firstLoadTriger: Observable<Void>,
        refreshTriger: Observable<Void>,
        loadMoreTriger: Observable<Void>
        )) {
        super.init()
        /// 界面第一次初始化
        let _ =  Observable.of(
            input.firstLoadTriger,
            reloadTrigger.withLatestFrom(input.firstLoadTriger))
            .merge().map{ AYProductApi.products(page: 0, pageSize: 10)}.share(replay: 1)
            .emeRequestApiForArray(AYProductModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.hasNextPage.onNext(data.count == 10)
                    self.productsElements.onNext( data.filter({ (item) -> Bool in
                        item.desc.length > 0
                    }))
                    self.page = 1
                case .failure(let error):
                    self.refresherror.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        
        /// 下拉刷新事件源
        input.refreshTriger
            .map{ AYProductApi.products(page: 0, pageSize: 10)}
            .emeRequestApiForArray(AYProductModel.self,activityIndicator: pullLoading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    /**
                     *  如果返回的数据正好相同 则默认还有数据
                     */
                    self.hasNextPage.onNext(data.count == 10)
                    self.productsElements.onNext( data.filter({ (item) -> Bool in
                        item.desc.length > 0
                    }))
                    self.page = 1
                case .failure(let error):
                    self.refresherror.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        
        input.loadMoreTriger
            .map{ AYProductApi.products(page: self.page, pageSize: 10)}
            .emeRequestApiForArray(AYProductModel.self)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    /**
                     *  如果返回的数据正好相同 则默认还有数据
                     */
                    self.hasNextPage.onNext(data.count == 10)
                    do {
                        try self.productsElements.onNext( self.productsElements.value() + data.filter({ (item) -> Bool in
                            item.desc.length > 0
                        }))
                    } catch {
                    }
                    
                    /**
                     *  需要修改page 页数
                     */
                    self.page = self.page +  1
                    
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
    }
    
}
